<?php

namespace addons\TfFutures\backend\controllers;

use addons\TfFutures\common\models\DailiWithdraw;
use common\models\common\SearchModel;
use Yii;
use common\models\sys\Manager;

class ManagerController extends BaseController
{
    /**
     * 佣金提现
     * @return mixed|string
     */
    public function actionWithdraw()
    {
        //获取用户信息
        $model = Manager::findOne(Yii::$app->user->id);
        if (Yii::$app->request->isPost) {
            $model->load(Yii::$app->request->post());
            if ($model->withdraw_money <= 0) {
                return $this->message('申请金额有误', $this->redirect(Yii::$app->request->getReferrer()),
                    'error');
            }
            if ($model->money < $model->withdraw_money) {
                return $this->message('提现金额不能大于佣金总额',
                    $this->redirect(Yii::$app->request->getReferrer()), 'error');
            }
            $model->money -= $model->withdraw_money;
            if (!$model->save()) {
                return $this->message($this->getError($model),
                    $this->redirect(Yii::$app->request->getReferrer()), 'error');
            }
            $log             = new DailiWithdraw();
            $log->member_id  = $model->id;
            $log->money      = $model->withdraw_money;
            $log->created_at = date('Y-m-d H:i:s');

            return $log->save()
                ? $this->message('申请成功', $this->redirect(Yii::$app->request->getReferrer()),
                    'success')
                : $this->message($this->getError($log),
                    $this->redirect(Yii::$app->request->getReferrer()), 'error');
        }

        return $this->renderAjax('withdraw', [
            'model' => $model,
        ]);
    }

    /**
     * Lists all Identity models.
     * @return mixed
     */
    public function actionIndex()
    {

        $searchModel  = new SearchModel([
            'model'        => DailiWithdraw::class,
            'scenario'     => 'default',
            //'partialMatchAttributes' => ['status'], // 模糊查询
            'defaultOrder' => [
                'created_at' => SORT_DESC,
            ],
            'pageSize'     => $this->pageSize,
        ]);
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->query->andWhere(['=', 'status', 0])
                            ->andWhere(['<>', 'member_id', Yii::$app->user->id]);

        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel'  => $searchModel,
        ]);
    }

    /*
     * 审核
     * */
    public function actionStatus($id,$status){
        $model = DailiWithdraw::findOne($id);
        if (empty($model)) {
            return $this->message("该申请不存在", $this->redirect(['index']), 'error');
        }
        if ($status == -1) {
            //驳回申请需要将佣金返回账户
            $user = Manager::findOne($model->member_id);
            $user->money = bcadd($user->money,$model->money,4);
            if (!$user->save()) {
                $this->message('用户返佣失败', $this->redirect(['index']), 'error');
            }
        }
        $model->status = $status;
        $model->updated_at = date("Y-m-d H:i:s");
        $model->ctrl_uid = \Yii::$app->user->identity->id;

        return $model->save()
            ? $this->message('操作成功', $this->redirect(['index']), 'success')
            : $this->message($this->getError($model), $this->redirect(['index']), 'error');
    }

    /*
     * 日志
     * */
    public function actionLog()
    {
        $search = Yii::$app->request->get('keyword','');
        $searchModel = new SearchModel([
            'model'        => DailiWithdraw::class,
            'scenario'     => 'default',
            'relations' => ['manager'=>['username']],
            'defaultOrder' => [
                'created_at' => SORT_DESC,
            ],
            'pageSize'     => $this->pageSize,
        ]);
        //        $daili        = Yii::$app->services->sysManager->findChild([Yii::$app->user->id]);
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->query->andWhere(['<>', DailiWithdraw::tableName().'.status', 0]);
        $dataProvider->query->andFilterWhere(['like', 'username', $search]);

        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel'  => $searchModel,
            'keyword'      => $search,
        ]);
    }
}